/*
 * Copyright (C) Cátedra SAES-UMU 2011 <pedromateo@um.es>
 *
 * Android HCI Extractor is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Android HCI Extractor is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.mmi.android.instrumentation.viewproxies;

import java.util.HashMap;
import java.util.LinkedList;

import android.view.View;

/**
 * This class is used to manage a set of viewProxies.
 * 
 * @author pedro mateo (pedromateo@um.es)
 *
 */
public class ViewProxyManager {

	/**
	 * Constructor.
	 */
	public ViewProxyManager(){
		proxies_ = new HashMap<Class, BaseViewProxy>();
	}

	/**
	 * Adds a proxy
	 * @param p the proxy
	 * @throws ProxyConflictException
	 */
	public void addProxy(BaseViewProxy p) throws ProxyConflictException{
		LinkedList<Class> adapted = p.getAdaptedClasses();
		if (adapted != null){
			for (Class c : adapted){
				if (proxies_.get(c) != null)
					throw new ProxyConflictException();
				proxies_.put(c, p);
			}
		}
	}

	/**
	 * Return a proxy depending on the view to adapt
	 * @param v the view object
	 * @return a proxy
	 */
	public BaseViewProxy getProxy(View v){
		return proxies_.get(v.getClass());
	}

	/**
	 * proxies map
	 */
	private HashMap<Class, BaseViewProxy> proxies_;

	/**
	 * Exception used to describe a proxy conflict, i.e. a
	 * class is adapted by more than one proxy.
	 * 
	 * @author pedro mateo (pedromateo@um.es)
	 *
	 */
	public class ProxyConflictException extends Exception{
		public ProxyConflictException(){}
	}
}
